package com.book.service.impl;

import com.book.mapper.RoleMenuDao;
import com.book.model.Role;
import com.book.mapper.RoleDao;
import com.book.model.RoleMenu;
import com.book.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wx
 * @since 2023-10-11 23:56:02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleMenuDao roleMenuDao;
    @Override
    public List<Role> getAll() {
        List<Role> roleList = roleDao.getAll();

        return roleList;
    }

    @Override
    @Transactional
    public void addRole(Role role, Long[] mids) {
        roleDao.insert(role);
        for (Long mid : mids) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRid(role.getRid());
            roleMenu.setMid(mid);
            roleMenuDao.insert(roleMenu);
        }
    }

    @Override
    public void updateRole(Role role, Long[] mids) {
        Long rid = role.getRid();
        roleMenuDao.deleteByMid(rid);
        for (Long mid : mids) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMid(mid);
            roleMenu.setRid(rid);
            roleMenuDao.insert(roleMenu);

        }

        roleDao.updateById(role);

    }

    @Override
    @Transactional
    public void deleteRole(Long rid) {
        roleMenuDao.deleteByMid(rid);
        roleDao.deleteById(rid);
    }
}
